package main

import (
	"GoAdmin/routers"
	"GoAdmin/service"
	"GoAdmin/utils/cache"
	"log"

	"github.com/beego/beego/v2/core/config"
	"github.com/spf13/cast"
)

func init() {
	// models.Setup()
	service.CasBinSetup()
	initRedis()
}

func initRedis() {
	conf, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		log.Fatal("配置文件载入错误")
	}

	redisSection, err := conf.GetSection("redis")
	if err != nil {
		log.Fatal("配置文件载入错误-数据库-redis载入失败")
	}

	redisConfig := cache.RedisConfig{
		Host:        redisSection["host"],
		Port:        cast.ToInt(redisSection["port"]),
		Index:       cast.ToInt(redisSection["index"]),
		Password:    redisSection["password"],
		MaxIdle:     cast.ToInt(redisSection["maxIdle"]),
		IdleTimeout: cast.ToInt(redisSection["idleTimeout"]),
		MaxActive:   cast.ToInt(redisSection["maxActive"]),
	}
	cache.SharedRedis().Init(redisConfig)
}

func main() {
	log.SetFlags(log.Lshortfile | log.LstdFlags)
	newRouter := routers.InitRouter()
	err := newRouter.Run(":8087")
	if err != nil {
		return
	}
}
